//
//  MainViewController.m
//  SportsApp
//
//  Created by Kevin Wong on 2012-12-05.
//  Copyright (c) 2012 Matthew Moritz. All rights reserved.
//

#import "MainViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

@synthesize slideShow;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.

    [self fade];    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)fade
{
    // The lower image, that will be visible after the fading effect
    layer1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Hockey1.png"] ];
    
    // Start with this layer fully transparent
    layer1.alpha = 0;
    [slideShow addSubview:layer1];
    
    // The upper layer will transition from alpha 1 to 0
    layer2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Hockey3.png"]];

    [slideShow addSubview:layer2];
    
    // Call the timer every one hundreth of a second
    timer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(fadeTimerEvent) userInfo:nil repeats:YES];
}


// Rate of transition for alpha values
#define FADE_IN_RATE		2.0/1000.0
#define FADE_OUT_RATE		3.0/1000.0

- (void)fadeTimerEvent
{
    if (layer1.alpha >= 1)
    {
        // At this point, layer1 is now visible
        [timer invalidate];
        timer = nil;
    }
    else
    {
        // Fade lower layer in (increase alpha)
        layer1.alpha += FADE_IN_RATE;
        
        // Fade upper layer out (decrease alpha)
        layer2.alpha -= FADE_OUT_RATE;
    }
}

@end
